home *** CD-ROM | disk | FTP | other *** search
/ Hacker's Secrets 4 / Hacker's Secrets 4.iso / internet / cgi-ex_1.txt < prev    next >
Text File  |  1996-04-26  |  3KB  |  100 lines

  1. Subject:      CGI security: Escape newlines.
  2. X-To:         bugtraq@CRIMELAB.COM
  3. X-cc:         www-security@ns2.Rutgers.EDU
  4. To: Multiple recipients of list BUGTRAQ <BUGTRAQ@crimelab.com>
  5.  
  6. When sending user-supplied data to a shell in a CGI program, it has
  7. become common practice among security-conscious CGI authors to remove
  8. or escape certain shell metacharacters to avoid them being interpreted
  9. by the shell and possibly allowing the user to execute arbritrary
  10. commands at his or her will.
  11.  
  12. There are a good set of security guidelines at:
  13. http://www.cerf.net/~paulp/cgi-security/safe-cgi.txt:
  14.  
  15. That document recommends removing or escaping the following characters
  16. in user-supplied data before passing it to a shell:
  17.  
  18.         ;<>*|`&$!#()[]{}:'"/
  19.  
  20. There is (at least) one character missing from this list: the new line
  21. character.  I have never seen the new line character included in a list
  22. of metacharaters to filter.
  23.  
  24. A sampling of widely-available CGI programs turned up many that are
  25. vulnerable.  Just about any CGI program which "un-hexifies" (converts
  26. characters represented by their hex values in a URL by to their actual
  27. character) its input and passes that input to a shell is likely to be
  28. vulnerable.
  29.  
  30. Here's a toy example:
  31.  
  32.   #!/usr/local/bin/perl
  33.   # usage: http://your.host/cgi-bin/echo?<string>
  34.   # Echos back the QUERY_STRING to the user.
  35.  
  36.   $| = 1;
  37.   $in = $ENV{'QUERY_STRING'};
  38.   $in =~ s/%(..)/pack("c",hex($1))/ge;
  39.  
  40.   # Escape the nasty metacharacters
  41.   # (List courtesy of http://www.cerf.net/~paulp/cgi-security/safe-cgi.txt)
  42.   $in =~ s/([;<>\*\|`&\$!#\(\)\[\]\{\}:'"])/\\$1/g;
  43.  
  44.   print "Content-type: text/html\n\n";
  45.   system("/bin/echo $in");
  46.  
  47. Install this program in cgi-bin/echo and
  48. <http://your.host/cgi-bin/echo?hello%20there>, will return a page
  49. containing the text "hello there".
  50.  
  51. Insert %0A, the newline character, and you can exploit the shell
  52. to run any command you wish.
  53.  
  54. For example, the URL <http://your.host/cgi-bin/echo?%0Acat%20/etc/passwd>
  55. will get you the password file.
  56.  
  57. (In Perl, the call to system() should have broken up the arguments:
  58.         system("/bin/echo", $in);
  59. and the problem would disappear.)
  60.  
  61. While this example uses system() in Perl, the general program will
  62. show up whenever a shell is invoked.
  63.  
  64. THE FIX:
  65.  
  66. Very simple.  Add the character \n (the new line character) to the
  67. list of characters to REMOVE from user-supplied data before
  68. suppling it to a shell in a CGI program.
  69.  
  70.   #!/usr/local/bin/perl
  71.   # usage: http://your.host/cgi-bin/safe-echo?<string>
  72.   # Echos back the QUERY_STRING to the user.
  73.  
  74.   $| = 1;
  75.   $in = $ENV{'QUERY_STRING'};
  76.   $in =~ s/%(..)/pack("c",hex($1))/ge;
  77.  
  78.   # Escape the nasty metacharacters
  79.   # (List courtesy of http://www.cerf.net/~paulp/cgi-security/safe-cgi.txt)
  80.   $in =~ s/([;<>\*\|`&\$!#\(\)\[\]\{\}:'"\n])/\\$1/g;
  81.  
  82.   # SECURITY FIX: REMOVE NEWLINES
  83.   $in =~ tr/\n//d;
  84.  
  85.   print "Content-type: text/html\n\n";
  86.   system("/bin/echo $in");
  87.  
  88. Again, this bug exists in MANY CGI programs.  If you maintain CGI
  89. programs on your server, I suggest you check through each of them.
  90. I've only looked through several CGI programs, and found the bug on
  91. some of them (the authors have been contacted). If I have more time in
  92. the near future, I'll post a list of vulnerable programs as well as
  93. alerting he authors.  In the meantime, you should check through the
  94. source of all of your CGI programs for this bug.
  95.  
  96. --
  97. Jennifer Myers                          http://www.eecs.nwu.edu/~jmyers/
  98.  
  99. 
  100.